package com.gxl.butt.config;

import com.gxl.butt.interceptor.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    @Bean
    public IpInterceptor ipInterceptor() {
        return new IpInterceptor();
    }

    @Bean
    public TokenInterceptor tokenInterceptor() {
        return new TokenInterceptor();
    }

    @Bean
    public ZKLockInterceptor zKLockInterceptor() {
        return new ZKLockInterceptor();
    }

    @Bean
    public BadIpInterceptor badIpInterceptor() {
        return new BadIpInterceptor();
    }

    /**
     * 拦截器
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {

        /**
         * 异常用户请求拦截,阻止cc攻击，多线程循环请求导致内存耗尽，服务器无法继续正常提供服务
         */
//        registry.addInterceptor(badIpInterceptor()).addPathPatterns("/**").excludePathPatterns("/api/ButtNotice").excludePathPatterns("/harduser/getTenUser");
        /**
         * 分布式锁 拦截
         */
        registry.addInterceptor(zKLockInterceptor()).addPathPatterns("/wxsys/createTopic");
        /**
         * ip白名单拦截
         */
        registry.addInterceptor(ipInterceptor()).addPathPatterns("/api/ButtNotice");
        /**
         * token凭证拦截
         */
        registry.addInterceptor(tokenInterceptor()).addPathPatterns("/api/ButtNotice");
        /**
         * 登录拦截
         */
//        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/sys/**");

        super.addInterceptors(registry);

    }
    /**
     * 发现如果继承了WebMvcConfigurationSupport，则在yml中配置的相关内容会失效。
     * 需要重新指定静态资源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

        super.addResourceHandlers(registry);
    }

    /**
     * 配置servlet处理
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}